<?php
namespace app\admin\controller;
use app\BaseController;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;
class Cate
{
    public function cate()
    {
        $parentid= Request::get('id',0);

        $list = Db::table('ecm_category')->where('parentid','=',"$parentid")
            ->order('sort')
            ->select()->toArray();


        //方案一：按照父ID进行判断
//        $list1 = Db::table('ecm_category')->distinct(true)->column('parentid');
//        View::assign('list1',$list1);
//        foreach ($list as $k=>$v){
//            $list[$k]['son']=Db::table('ecm_category')->where('parentid','=',"$parentid")->count();
//        }
        //方案二：
        if (Cache::has('all')){
            $all = Cache::get('all');
        }else{
            $all = Db::name('category')->select()->toArray();
            Cache::set('all',$all,10);
        }
        foreach ($list as &$v){
            foreach ($all as $vv) {
                if ($v['id'] == $vv['parentid']){
                    $v['son'] = 1;
                }

            }
        }
        View::assign('list',$list);
        return view();
    }

    public function CateEdit()
    {
        return view();
    }
}